import wx from 'weixin-js-sdk'; // 微信SDK依赖
import { getWeChatJsSDKConfig } from '@/until/tools.js'
import setting from "@/common/config.js"; 

// const jsApiList = ["onMenuShareAppMessage", "onMenuShareTimeline"];
export function getShareJSSDK(data, dataForWeixin) {
	console.log(data, 'data----------')
	console.log(dataForWeixin, 'dataForWeixin----------')
	return new Promise((resolve, reject) => {
		// 配置微信SDK
		wx.config({
			debug: false, // 开启调试模式
			appId: data.appId, // 必填，公众号的唯一标识
			timestamp: data.timestamp, // 必填，生成签名的时间戳
			nonceStr: data.nonceStr, // 必填，生成签名的随机串
			signature: data.signature, // 必填，签名
			jsApiList: ["onMenuShareAppMessage", "onMenuShareTimeline"], // 必填，需要使用的JS接口列表
			success: () => {
				console.log("微信JS-SDK配置成功");
				resolve(); // 配置成功
			},
			fail: (err) => {
				console.error("微信JS-SDK配置失败", err);
				reject(err); // 配置失败
			}
		});

		wx.ready(function() {
			// 分享给朋友
			wx.onMenuShareAppMessage({
				title: dataForWeixin.title || '测试的',
				desc: dataForWeixin.desc || '看看',
				link: dataForWeixin.link || 'www.baidu.com',
				imgUrl: dataForWeixin.imgUrl,
				trigger: function(res) {
					console.log('分享给朋友触发', res);
				},
				success: function(res) {
					console.log('已分享', res);
				},
				cancel: function(res) {
					console.log('已取消', res);
				},
				fail: function(res) {
					console.error('分享失败', res);
				},
			});

			// 分享到朋友圈
			wx.onMenuShareTimeline({
				title: dataForWeixin.title,
				link: dataForWeixin.link,
				imgUrl: dataForWeixin.imgUrl,
				trigger: function(res) {
					console.log('分享到朋友圈触发', res);
				},
				success: function(res) {
					console.log('已分享', res);
				},
				cancel: function(res) {
					console.log('已取消', res);
				},
				fail: function(res) {
					console.error('分享失败', res);
				},
			});
		});
		// wx.error(function (err) {
		//   console.error("微信SDK初始化错误", err);
		//   reject(err);  // 初始化失败
		// });
	});
}

// h5微信公众号分享
export function getShareApp() {
	const shareData = ref({
		title: '分享标题',
		desc: '分享描述',
		link: `${setting.APP_PATH}?org=${uni.getStorageSync('shopInfo')?.orgCode}&brd=${uni.getStorageSync('shopInfo')?.brandCode}`,
		imgUrl: 'https://jtmc.oss-cn-hangzhou.aliyuncs.com/wmp/qnjg/logo.jpg'
	});
	// const getShare = async () => {
	getWeChatJsSDKConfig().then(resking => {
		// console.log(resking, 'SDKConfig.value')
		if (resking) {
			// console.log(shareData.value, 'shareData.link')
			getShareJSSDK(resking, shareData.value)
		}
	})
	// }
}